using UnityEngine;
using System.Collections;

public class EnemyManager : MonoBehaviour {

	public Transform[] enemies;
	public Transform[] spawnPoints = new Transform[16];
    public GameObject spawnPointsContainer;

	public float timerInstance;
	private float timer;

    private int i = 0;

    public TextMesh waveTimeText;
    public float timeToWave = 10;

	void Start ()
    {
        SetSpawnPoints();    
	}

    void SetSpawnPoints()
    {
        Transform[] potencialSpawnPoints = spawnPointsContainer.GetComponentsInChildren<Transform>();

        foreach (Transform sP in potencialSpawnPoints)
        {
            if (sP != spawnPointsContainer.transform)
            {
                spawnPoints[i] = sP;
                i++;
            }
        }
    }
	
	void Update () 
	{
        waveTimeText.text = "Time to Next Wave: " + (int)timeToWave;

		if(CharMovement.playing)
		{
			timer += Time.deltaTime;
            timeToWave -= Time.deltaTime;

			if(timer >= timerInstance)
			{
                for(int i = 0; i < 5; i++)
                {
                    createEnemies(enemies[Random.Range(0, enemies.Length)], spawnPoints[Random.Range(0, spawnPoints.Length)].position); 
                }
				
				timer = 0;
                timeToWave = 10;
			}
		}
	}
	
	void createEnemies(Transform enemyType, Vector3 pos)
	{
		Instantiate(enemyType,pos,enemyType.rotation);
	}
}
